<?php

/**
 * EmsValue form.
 *
 * @package    ems1
 * @subpackage form
 * @author     Jacobo Martínez
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class EmsValueForm extends BaseEmsValueForm
{
    public function configure()
    {
        //$this->widgetSchema['register_id'] = new sfWidgetFormInputHidden();
        unset ($this['register_id']);

        $this->widgetSchema['field_id'] = new sfWidgetFormInputHidden();

        switch ($this->getObject()->getField()->getFieldType()->getType()) {
            case 'shorttext':
                $this->widgetSchema['value'] = new sfWidgetFormInputText(array('label' => $this->getObject()->getField()->getTitle()));
                $this->validatorSchema['value'] = new sfValidatorRegex(
                        array(
                            'pattern' => "/^([a-zA-Z\á\é\í\ó\ú\Á\É\Í\Ó\Ú\ñ\Ñ\s]*?)$/",
                            'max_length' => $this->getObject()->getField()->getFieldType()->getLength(),
                            'trim' => true,
                        ),
                        array(
                            'invalid' => 'Solo puede contener letras mayúsculas o minúsculas y espacios en blanco.',
                            'max_length' => 'Excede el limite de caracteres (máximo %max_length% caracteres).'
                        ));
                if($this->getObject()->getField()->getIsRequired()){
                    $this->validatorSchema['value']->setOption('required', true);
                    $this->validatorSchema['value']->setMessage('required', 'Campo obligatorio.');
                }
                $this->widgetSchema->setHelp('value', 'Solo letras mayúsculas o minúsculas y espacios en blanco.');
                break;
            case 'longtext':
                $this->widgetSchema['value'] = new sfWidgetFormInputText(array('label' => $this->getObject()->getField()->getTitle()));
                $this->validatorSchema['value'] = new sfValidatorRegex(
                        array(
                            'pattern' => "/^([a-zA-Z\á\é\í\ó\ú\Á\É\Í\Ó\Ú\ñ\Ñ\s]*?)$/",
                            'max_length' => $this->getObject()->getField()->getFieldType()->getLength(),
                            'trim' => true,
                        ),
                        array(
                            'invalid' => 'Solo puede contener letras mayúsculas o minúsculas  y espacios en blanco.',
                            'max_length' => 'Excede el limite de caracteres (máximo %max_length% caracteres).'
                        ));
                if($this->getObject()->getField()->getIsRequired()){
                    $this->validatorSchema['value']->setOption('required', true);
                    $this->validatorSchema['value']->setMessage('required', 'Campo obligatorio.');
                }
                $this->widgetSchema->setHelp('value', 'Solo letras mayúsculas o minúsculas y espacios en blanco.');
                break;
            case 'email':
                $this->widgetSchema['value'] = new sfWidgetFormInputText(array('label' => $this->getObject()->getField()->getTitle()));
                $this->validatorSchema['value'] = new sfValidatorEmail(
                        array(
                            'max_length' => $this->getObject()->getField()->getFieldType()->getLength(),
                            'trim' => true,
                        ),
                        array(
                            'max_length' => 'La dirección "%value%" excede el limite de caracteres (máximo %max_length% caracteres).',
                            'invalid'    => 'La dirección "%value%" no tiene formato de correo electrónico válido.',
                        ));
                if($this->getObject()->getField()->getIsRequired()){
                    $this->validatorSchema['value']->setOption('required', true);
                    $this->validatorSchema['value']->setMessage('required', 'Campo obligatorio.');
                }
                $this->widgetSchema->setHelp('value', 'Por ejemplo: correo@ejemplo.com');
                break;
            case 'number':
                $this->widgetSchema['value'] = new sfWidgetFormInputText(
                        array('label' => $this->getObject()->getField()->getTitle()));
                $this->validatorSchema['value'] = new sfValidatorRegex(
                        array(
                            'pattern' => "/^[[:digit:]]+$/",
                            'max_length' => $this->getObject()->getField()->getFieldType()->getLength(),
                            'trim' => true,
                        ),
                        array(
                            'invalid' => 'Solo puede contener números.',
                            'max_length' => 'Excede el limite de caracteres (máximo %max_length% caracteres).'
                        ));
                if($this->getObject()->getField()->getIsRequired()){
                    $this->validatorSchema['value']->setOption('required', true);
                    $this->validatorSchema['value']->setMessage('required', 'Campo obligatorio.');
                }
                $this->widgetSchema->setHelp('value', 'Solo números enteros (0-9).');
                break;
            case 'alphanumeric':
                $this->widgetSchema['value'] = new sfWidgetFormInputText(array('label' => $this->getObject()->getField()->getTitle()));
                $this->validatorSchema['value'] = new sfValidatorRegex(
                        array(
                            'pattern' => "/^([a-zA-Z0-9\á\é\í\ó\ú\Á\É\Í\Ó\Ú\ñ\Ñ\s]*?)$/",
                            'max_length' => $this->getObject()->getField()->getFieldType()->getLength(),
                            'trim' => true,
                        ), array(
                            'invalid' => 'Solo puede contener números, letras mayúsculas y minúsculas y espacios en blanco.',
                            'max_length' => 'Excede el limite de caracteres (máximo %max_length% caracteres).'
                        ));
                if($this->getObject()->getField()->getIsRequired()){
                    $this->validatorSchema['value']->setOption('required', true);
                    $this->validatorSchema['value']->setMessage('required', 'Campo obligatorio.');
                }
                $this->widgetSchema->setHelp('value', 'Solo números (0-9) o letras mayúsculas y minúsculas y espacios en blanco.');
                break;
            case 'telephone':
                $this->widgetSchema['value'] = new sfWidgetFormPhoneNumber(
                        array('label' => $this->getObject()->getField()->getTitle()));
                $this->validatorSchema['value'] = new sfValidatorPhoneNumber(
                        array(
                            'max_length' => $this->getObject()->getField()->getFieldType()->getLength(),
                            'trim' => true,
                        ),
                        array(
                            'invalid' => 'No ha introducido un número telefónico válido.',
                            'max_length' => 'Excede el limite de caracteres (máximo %max_length% caracteres).'
                        ));
                if($this->getObject()->getField()->getIsRequired()){
                    $this->validatorSchema['value']->setOption('required', true);
                    $this->validatorSchema['value']->setMessage('required', 'Campo obligatorio.');
                }
                $this->widgetSchema->setHelp('value', 'Por ejemplo: 04149990000.');
                break;
            case 'date':
                $this->widgetSchema['value'] = new sfWidgetFormDateForMobile();
                $this->validatorSchema['value'] = new sfValidatorDate(
                        array(
                            'date_output' => 'd-m-Y',
                            ),
                        array('invalid' => 'Ha colocado una fecha inválida.')
                        );
                if($this->getObject()->getField()->getIsRequired()){
                    $this->validatorSchema['value']->setOption('required', true);
                    $this->validatorSchema['value']->setMessage('required', 'Campo obligatorio.');
                }
                $this->widgetSchema->setHelp('value', 'Fecha: dd/mm/aaaa.');
                break;
            case 'password':
                $this->widgetSchema['value'] = new sfWidgetFormInputPassword(array('label' => $this->getObject()->getField()->getTitle()));
                $this->validatorSchema['value'] = new sfValidatorString(
                        array(
                            'max_length' => $this->getObject()->getField()->getFieldType()->getLength(),
                            'trim' => true,
                        ),
                        array(
                            'max_length' => 'Excede el limite de caracteres (máximo %max_length% caracteres).'
                        ));
                if($this->getObject()->getField()->getIsRequired()){
                    $this->validatorSchema['value']->setOption('required', true);
                    $this->validatorSchema['value']->setMessage('required', 'Campo obligatorio.');
                }
                break;
            case 'idnumber':
                $this->widgetSchema['value'] = new sfWidgetFormInputText(
                        array('label' => $this->getObject()->getField()->getTitle()),
                        array('maxlength' => 8)
                        );
                $this->validatorSchema['value'] = new sfValidatorRegex(
                        array(
                            'pattern' => "/^[[:digit:]]+$/",
                            'min_length' => 4,
                            'max_length' => 8,
                            'trim' => true,
                        ),
                        array(
                            'invalid' => 'Solo puede contener números (0-9) y debe tener mínimo 4 digitos y máximo 8.',
                            'min_length' => 'Debe tener al menos 4 caracteres.',
                            'max_length' => 'Excede el limite de caracteres (máximo 8 caracteres).'
                        ));
                if($this->getObject()->getField()->getIsRequired()){
                    $this->validatorSchema['value']->setOption('required', true);
                    $this->validatorSchema['value']->setMessage('required', 'Campo obligatorio.');
                }
                $this->widgetSchema->setHelp('value', 'Número de cédula, por ejemplo 56789012.');
                break;
            case 'select':
                $options = explode(',',$this->getObject()->getField()->getOptions());
                $this->widgetSchema['value'] = new sfWidgetFormChoice(
                        array(
                            'choices' => array_combine($options, $options),
                            'label' => $this->getObject()->getField()->getTitle()
                        ));
                $this->validatorSchema['value'] = new sfValidatorChoice(array('choices' => $options));
                if($this->getObject()->getField()->getIsRequired()){
                    $this->validatorSchema['value']->setOption('required', true);
                    $this->validatorSchema['value']->setMessage('required', 'Campo obligatorio.');
                }
                break;
            default:
                break;
        }

        if($this->getObject()->getField()->getIsUnique() && !$this->getOption('for_user_authentication')){
            $this->validatorSchema->setPostValidator(
                    new sfValidatorDoctrineUnique(
                        array('model' => 'EmsValue', 'column' => array('value','field_id')),
                        array('invalid' => 'Este valor ya ha sido registrado.')
                    )
                );
        }
    }
}
